package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ShebeiType;
import ywpt.web.common.PublicFunction;
@Service("shebeiTypeService")
@RemoteProxy(name="shebeiTypeService")
public class ShebeiTypeServiceImpl extends HibernateEntityDao<ShebeiType> implements ShebeiTypeService{
	public ShebeiTypeServiceImpl(){
		
	}

	@Override
	public String CreateTypeCode() {
		DetachedCriteria query = DetachedCriteria.forClass(ShebeiType.class, "shebeiType");
		String typeCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<ShebeiType> shebeiTypes = this.getShebeiTypes(query);
		if ( shebeiTypes.iterator().hasNext() )	
		{
			code = shebeiTypes.iterator().next().getCode();
			maxId = new Integer(code);
		}
		typeCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return typeCode;
	}

	@Override
	public ShebeiType getShebeiTypeById(Long id) {
		return this.get(id);
	}

	@Override
	public int getShebeiTypeCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ShebeiType> getShebeiTypes(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<ShebeiType> getShebeiTypes() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<ShebeiType> getShebeiTypesByTypeName(String type, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ShebeiType.class, "shebeiType");
		if ( type != null && !type.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("type", "%" + type + "%"),  
							          Restrictions.like("code", "%" + type + "%")));
		}
		query.addOrder(Order.asc("code"));
		List<ShebeiType> shebeiTypeList = new ArrayList<ShebeiType>();
		List<ShebeiType> shebeiTypeLists = new ArrayList<ShebeiType>();
		ShebeiType shebeiType =new ShebeiType();
		
		shebeiTypeList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ShebeiType> itr = shebeiTypeList.iterator(); itr.hasNext(); i++ ) 
		{
			shebeiType = itr.next();
			String ss = PublicFunction.format(shebeiType.getCode() + "", 6, "  ", 1)
			          + shebeiType.getType(); 
			
			shebeiType.setType(ss);
			shebeiTypeLists.add(shebeiType);
		}
		return shebeiTypeLists;
	}

	@Override
	@RemoteMethod
	public int getShebeiTypesCountByName(String type) {
		DetachedCriteria query = DetachedCriteria.forClass(ShebeiType.class, "shebeiType");
		if ( type != null && !type.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("type", "%" + type + "%" ),  
							          Restrictions.like("code", "%" + type + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	@Override
	public void removeShebeiType(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveShebeiType(ShebeiType shebeiType) {
		super.save(shebeiType);
	}

	@Override
	public void updateShebeiType(ShebeiType shebeiType) {
		super.update(shebeiType);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ShebeiType> getShebeiTypes(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}
}

